package cn.hongyu.controller;

import cn.hongyu.controller.results.Code;
import cn.hongyu.controller.results.Result;
import cn.hongyu.domain.User;
import cn.hongyu.exception.BusinessException;
import cn.hongyu.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService  userService;

    @GetMapping("/{page}/{size}")
    public Result findAll(@PathVariable Integer page,@PathVariable Integer size){
        PageInfo<User> all = userService.findAll(page, size);
        if(page>10) throw new BusinessException("出错了",Code.GET_ERR);
        return new Result(null !=all?Code.GET_OK:Code.GET_ERR,all);
    }

    @PostMapping
    public Result save(User user){
        boolean flag = userService.save(user);
        return new Result(flag? Code.SAVE_OK:Code.SAVE_ERR);
    }
    @PutMapping
    public Result update(User user){
        boolean flag = userService.update(user);
        return new Result(flag? Code.UPDATE_OK:Code.UPDATE_ERR);
    }
    @DeleteMapping("/{uuid}")
    public Result delete(@PathVariable Integer uuid){
        boolean flag = userService.delete(uuid);
        return new Result(flag? Code.DELETE_OK:Code.DELETE_ERR);
    }
    @PostMapping("/login/{userName}/{password}")
    public Result login(@PathVariable String userName,@PathVariable String password){
        User user = userService.login(userName, password);
        return new Result(null !=user? Code.SAVE_OK:Code.SAVE_ERR,user);
    }

}
